package com.atguigu.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }
    /**
     * @param:
     * @return： java.util.List<com.atguigu.gulimall.product.entity.CategoryEntity>
     * @exception：
     * @author： chenfeng
     * @date： 2020/4/6 12:32
     */
    @Override
    public List<CategoryEntity> listWithTree() {
        // 1.查出所有分类
            List<CategoryEntity> categoryEntities = baseMapper.selectList(null);

        // 2.组装父子的树形结构
        // 2.1找到所有一级分类
        List<CategoryEntity> leave1Menus = categoryEntities.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(menu -> {
                  menu.setChildren(getChildren(menu, categoryEntities));
                  return menu;
                })
                .sorted((num1, num2) -> num1.getSort()==null?0:num1.getSort()-(num2.getSort()==null?0:num2.getSort()))
                .collect(Collectors.toList());
        // 2.2
        return leave1Menus;
    }

    @Override
    public void removeMenuBiIds(List<Long> asList) {
        // TODO 检查当前删除的菜单，是否被别的地方引用

        // 逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatlogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);

        return  parentPath.toArray(new Long[parentPath.size()]);
    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        // 收集当前节点的id
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid() != 0) {
            findParentPath(byId.getParentCid(), paths);
        }
        return paths;
    }

    /**
     * @param: root
     * @param: all
     * @return： java.util.List<com.atguigu.gulimall.product.entity.CategoryEntity>
     * @exception：递归查找所有菜单的子菜单
     * @author： chenfeng
     * @date： 2020/4/6 12:57
     */
    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> all) {
           return all.stream()
                    .filter(categoryEntity -> categoryEntity.getParentCid().equals(root.getCatId()))
                    .map(categoryEntit -> {
                        categoryEntit.setChildren(getChildren(categoryEntit, all));
                        return categoryEntit;

                    })
                    .sorted((num1, num2) -> num1.getSort()==null?0:num1.getSort()-(num2.getSort()==null?0:num2.getSort()))
                    .collect(Collectors.toList());
    }
}